import {
    login,
    logout,
    getInfo,
    generateAuthCode,
    register,
    verifyUsername,
    verifyTelephone,
    productHome,
    taskAccordion
} from '../api/'
import {setToken, removeToken} from '../assets/js/auth'

const actions = {
    Login({commit}, userInfo) {
        let loginInfo = {};
        if (userInfo.isPhoneSignIn) {
            loginInfo.telephone = userInfo.phoneNumber;
            loginInfo.code = userInfo.authCode;
        } else {
            loginInfo.username = userInfo.username;
            loginInfo.password = userInfo.password;
        }
        loginInfo.isPhoneSignIn = userInfo.isPhoneSignIn
        return new Promise((resolve, reject) => {
            login(loginInfo).then(response => {
                const data = response.data;
                const tokenStr = data.tokenHead + data.token;
                commit('SET_TOKEN', tokenStr);
                setToken('loginToken', tokenStr);
                setToken('login', true);
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    // 获取用户信息
    GetInfo({commit, state}) {
        return new Promise((resolve, reject) => {
            getInfo().then(response => {
                const data = response.data;
                commit('RECORD_USERINFO', {info: data});
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    // 登出
    LogOut({commit, state}) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                commit('SET_TOKEN', '');
                commit('RECORD_USERINFO', null);
                removeToken('loginToken');
                setToken('login', false);
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    GenerateAuthCode({commit}, telephone) {
        return new Promise((resolve, reject) => {
            generateAuthCode(telephone).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    Register({commit}, regMsg) {
        return new Promise((resolve, reject) => {
            register(regMsg).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    VerifyUsername({commit}, username) {
        return new Promise((resolve, reject) => {
            verifyUsername(username).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    VerifyTelephone({commit}, telephone) {
        return new Promise((resolve, reject) => {
            verifyTelephone(telephone).then(() => {
                resolve();
            }).catch(error => {
                reject(error);
            })
        })
    },
    ProductHome({commit}) {
        return new Promise((resolve, reject) => {
            productHome().then(res => {
                resolve(res);
            }).catch(error => {
                reject(error);
            })
        })
    },
    TaskAccordion({commit}) {
        return new Promise((resolve, reject) => {
            taskAccordion().then(res=>{
                resolve(res);
            }).catch(error=>{
                reject(error);
            })
        })
    }
}
export default actions
